package com.example.springboot.controller;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot.common.AuthAccess;
import com.example.springboot.common.Page;
import com.example.springboot.common.Result;
import com.example.springboot.entity.User;
import com.example.springboot.exception.ServiceException;
import com.example.springboot.mapper.UserMapper;
import com.example.springboot.service.UserService;
import com.example.springboot.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static net.sf.jsqlparser.util.validation.metadata.NamedObject.user;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

     /*
     新增用户信息
      */
    @AuthAccess
    @PostMapping("/add")
    public Result add(@RequestBody User user) {
        try {
            userService.insertUser(user);
        } catch (Exception e){
            if (e instanceof DuplicateKeyException){
                return Result.error("插入数据库错误");
            }else {
                return Result.error("系统错误");
            }
        }
        return Result.success();
    }
    /*
    修改用户信息
     */
    @PutMapping("/update")
    public Result update(@RequestBody User user){
        userService.updateUser(user);
        return Result.success();
    }
    /*
    删除用户信息
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        userService.deleteUser(id);
        return Result.success();
    }
    /*
    批量删除用户信息
     */
    @DeleteMapping("/delete/batch")
    public Result batchDelete(@RequestBody List<Integer> ids){
        userService.batchDeleteUser(ids);
        return Result.success();
    }
    /*
    查询全部用户信息
    */
    @AuthAccess
    @GetMapping("/selectAll")
    public Result selectAll(){
        List <User> userList = userService.selectAll();
        return Result.success(userList);
    }
    /*
    根据id查询
    */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id){
        User user = userService.selectById(id);
        return Result.success(user);
    }
    /*
    根据name查询
    */
    @GetMapping("/selectByName/{name}")
    public Result selectByName(@PathVariable String name){
        List<User> userList = userService.selectByName(name);
        return Result.success(userList);
    }
    /*
    多条件查询
    */
    @GetMapping("/selectByMore")
    public Result selectByMore(@RequestParam String username, @RequestParam String name){
        List<User> userList = userService.selectByMore(username, name);
        return Result.success(userList);
    }
    /*
    多条件模糊查询
    */
    @GetMapping("/selectByMo")
    public Result selectByMo(@RequestParam String username, @RequestParam String name){
        List<User> userList = userService.selectByMo(username, name);
        return Result.success(userList);
    }
    /*
    多条件查询
    */
    @GetMapping("/selectByPage")
    public Result selectByPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam String username,
                               @RequestParam String name){
        Page<User> page = userService.selectByPage(pageNum,pageSize,username, name);
        return Result.success(page);
    }

}
